from functools import wraps

from flask import g
from flask import session


def is_login(view_func):
    @wraps(view_func)
    def wrapper(*args, **kwargs):
        user_id = session.get("user_id")
        user = None

        if user_id:
            from models import User
            user = User.query.filter(User.id == user_id).first()

        g.user = user

        return view_func(*args, **kwargs)

    return wrapper
